{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Basic Loading of the Kernel"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To run the notebooks we recommend using Poetry and starting a shell with a virtual environment\n",
    "prepared to use SK. \n",
    "\n",
    "See [DEV_SETUP.md](../../../python/DEV_SETUP.md) for more information."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!python -m pip install -r requirements.txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import semantic_kernel as sk\n",
    "from semantic_kernel.ai.open_ai import AzureTextCompletion, OpenAITextCompletion"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can instantiate the kernel in a few ways, depending on your use case."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Simple instance\n",
    "kernel_1 = sk.Kernel()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Instance with a custom logger\n",
    "my_logger = sk.NullLogger()\n",
    "kernel_2 = sk.Kernel(log=my_logger)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Instance with a custom configuration\n",
    "my_config = sk.KernelConfig()\n",
    "kernel_2 = sk.Kernel(config=my_config)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When using the kernel for AI requests, the kernel needs some settings like URL and credentials to the AI models.\n",
    "\n",
    "The SDK currently supports OpenAI and Azure OpenAI, other services will be added over time.\n",
    "\n",
    "If you need an Azure OpenAI key, go [here](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/quickstart?pivots=rest-api)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "kernel = sk.Kernel()\n",
    "\n",
    "kernel.config.add_text_backend(               # We are adding a text backend\n",
    "    \"Azure_curie\",                            # The alias we can use in prompt templates' config.json\n",
    "    AzureTextCompletion(\n",
    "        \"my-finetuned-Curie\",                 # Azure OpenAI *Deployment ID*\n",
    "        \"https://contoso.openai.azure.com/\",  # Azure OpenAI *Endpoint*\n",
    "        \"...your Azure OpenAI Key...\"         # Azure OpenAI *Key*\n",
    "    )\n",
    ")\n",
    "\n",
    "kernel.config.add_text_backend(               # We are adding a text backend\n",
    "    \"OpenAI_davinci\",                         # The alias we can use in prompt templates' config.json\n",
    "    OpenAITextCompletion(\n",
    "        \"text-davinci-003\",                   # OpenAI Model Name\n",
    "        \"...your OpenAI API Key...\",          # OpenAI API key\n",
    "        \"...your OpenAI Org ID...\"            # *optional* OpenAI Organization ID\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When working with multiple backends and multiple models, the **first backend** defined\n",
    "is also the \"**default**\" used in these scenarios:\n",
    "\n",
    "* a prompt configuration doesn't specify which AI backend to use\n",
    "* a prompt configuration requires a backend unknown to the kernel\n",
    "\n",
    "The default can be set and changed programmatically:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "kernel.config.set_default_text_backend(\"Azure_curie\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Great, now that you're familiar with setting up the Semantic Kernel, let's see [how we can use it to run prompts](02-running-prompts-from-file.ipynb)."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  },
  "polyglot_notebook": {
   "kernelInfo": {
    "items": [
     {
      "aliases": [
       "frontend"
      ],
      "name": "vscode"
     }
    ]
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
